package cn.com.shbs.Controller;

import cn.com.shbs.Config.CommonUtil;
import cn.com.shbs.Config.Page;
import cn.com.shbs.Dao.Customer.CustomerDao;
import cn.com.shbs.Service.CustomerService.ICustomerService;
import cn.com.shbs.Service.code.SerialNumberGeneratorService;
import cn.com.shbs.model.Customer;
import cn.com.shbs.model.CustomerMedical;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

@RestController
@RequestMapping("API/")
public class CustomerController {
    static Logger logger = LoggerFactory.getLogger(CustomerController.class);
    @Autowired
    SerialNumberGeneratorService serialNumberGeneratorService;

    @Autowired
    ICustomerService iCustomerService;

    @Autowired
    CustomerDao customerDao;

    /*根据id查找用户信息*/
    @RequestMapping("/queryCustomer")
    public Object queryCustomer(HttpServletRequest request,
                                HttpServletResponse response,
                                Page page,
                                @RequestParam(value="customername",required=false)String customername){
        String message = "success";
        JSONObject result=new JSONObject();
        HashMap<String,Object> customermap=new HashMap<>();

        List<HashMap> customerList =new ArrayList<>();

        //总条数
        int countnum=0;
        try {
            //参数
            customermap.put("customername",customername);

            //查询符合条件的总条数
            countnum = customerDao.queryCustomerNum(customermap);
            //总条数
            page.setTotalCount(countnum);

            customermap.put("startIndex",page.getStartCount());

            customermap.put("pageSize", page.getPageSize());

            //通过参数map查询顾客list
            customerList = customerDao.queryCustomer(customermap);

        }catch (Exception e){
            message = "error";
            logger.error("UserInfoController.queryUserInfoListId()",e);
            e.printStackTrace();
        }
        result.put("customerList", JSONArray.fromObject(customerList));
        result.put("page", JSONObject.fromObject(page));
        result.put("message",message);
        return result.toString();
    }

    /**
     * 添加客户信息，客户基本医疗信息
     * @param request
     * @param customer 客户信息
     * @param customerMedical 客户基本医疗信息
     * @return
     */
    @RequestMapping(value = "/addcustomer",method = RequestMethod.POST)
    public Object addcustomer(HttpServletRequest request,
                              Customer customer, CustomerMedical customerMedical){

        String message="error";
        JSONObject result=new JSONObject();

        try {
            if(customer.getCustomer_name()!=null&&!"".equals(customer.getCustomer_name().trim())){

                //生成顾客id
                String customercode=  CommonUtil.PREFIX_CUSTOMER+serialNumberGeneratorService.getCodeGeneratorstr(CommonUtil.STEPNUM,CommonUtil.CUSTOMER_TYPE);
                //设置顾客id
                customer.setCustomer_code(customercode);

                //添加客户的创建时间
                customer.setCreattime(CommonUtil.getNowTime_tamp());
                //设置客户基本医疗信息
                customerMedical.setCustomer_code(customercode);
                //调用添加客户，客户基本医疗信息
                iCustomerService.addcustomer(customer,customerMedical);

                message="success";

            }else{
                customerMedical=null;
                //顾客名称为空
                message="nameisnull";

            }

            JSONObject customerjson=JSONObject.fromObject(customer);
            JSONObject customerMedicaljson=JSONObject.fromObject(customerMedical);
            result.put("customer",customerjson);
            result.put("customerMedical",customerMedicaljson);

        }catch (Exception e){
            logger.error("CustomerController.addcustomer()",e);
            e.printStackTrace();
        }


        result.put("message",message);
        return result.toString();
    }

    /**
     * 修改顾客信息，顾客基本医疗信息
     * @param request
     * @param response
     * @param customer
     * @param customerMedical
     * @return
     */
    @RequestMapping(value = "/updateCustomer",method = RequestMethod.POST)
    public Object updateCustomer(HttpServletRequest request,
                                 HttpServletResponse response,
                                 Customer customer,
                                 CustomerMedical customerMedical){
        String message="error";
        JSONObject result=new JSONObject();
        try{

            iCustomerService.updateCustomer(customer,customerMedical);
            message="success";
        }catch (Exception e){
            logger.error("CustomerController.updateCustomer()",e);
            e.printStackTrace();
        }

        result.put("message",message);
        return result.toString();
    }

    /**
     * 根据顾客code查询顾客信息
     * @param request
     * @param response
     * @param code 顾客code
     * @return
     */
    @RequestMapping(value = "/queryCustomerByCode",method = RequestMethod.POST)
    public Object queryCustomerByCode(HttpServletRequest request,
                                      HttpServletResponse response,
                                      @RequestParam(value="code",required=false)String code){
        String message="error";
        JSONObject result=new JSONObject();
        HashMap<String,Object> map=new HashMap<>();
        HashMap<String,Object> customerMap=new HashMap<>();

        try{
            map.put("code",code);
            customerMap=customerDao.queryCustomerByCode(map);

            message="success";
        }catch (Exception e){
            logger.error("CustomerController.queryCustomerByCode()",e);
            e.printStackTrace();
        }

        result.put("customer",JSONObject.fromObject(customerMap));
        result.put("message",message);
        return result.toString();
    }
}
